package com.wiretap.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * 联系人解析工具
 * 
 * @author Administrator
 * 
 */
public class ContactAnalyzeUtil {

	public static void main(String[] args) {
		System.out.println(analyze(new File("d:\\test.txt")));
	}

	public static String analyze(File file) {
		String filePath = file.getAbsolutePath();
		if (!CommenUtil.isEmpty(filePath)) {
			filePath = filePath.toLowerCase();
			// 文本文件
			if (filePath.endsWith(".txt")) {
				return readTxt(file);
			}// excel2003文件
			else if (filePath.endsWith(".xls")) {
				return readXls(file);
			}// excel2007文件
			else if (filePath.endsWith(".xlsx")) {
				return readXlsx(file);
			}
		}
		return null;
	}

	/**
	 * 解析excel2003文件
	 * 
	 * @param file
	 * @return
	 */
	private static String readXls(File file) {
		String result = null;
		try {
			StringBuffer sb = new StringBuffer();
			InputStream is = new FileInputStream(file);
			HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
			int numSheet = hssfWorkbook.getNumberOfSheets();
			if (numSheet > 0) {
				HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(0);
				if (hssfSheet != null) {
					// 循环行Row
					int rowCount = hssfSheet.getLastRowNum();
					for (int rowNum = 0; rowNum <= rowCount; rowNum++) {
						HSSFRow hssfRow = hssfSheet.getRow(rowNum);
						if (hssfRow == null) {
							continue;
						}
						int cellNum = hssfRow.getLastCellNum();
						if (cellNum > 0) {
							HSSFCell hssfCell = hssfRow.getCell(0);
							String val = getValue(hssfCell);
							if (!CommenUtil.isEmpty(val)) {
								val = val.trim();
								if (!CommenUtil.isEmpty(val)) {
									sb.append(val).append(",");
								}
							}
						}
					}
				}
			}
			// 去除最后一个“，”
			if (sb.length() > 0) {
				result = sb.substring(0, sb.lastIndexOf(","));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 解析excel2007文件
	 * 
	 * @param file
	 * @return
	 */
	private static String readXlsx(File file) {
		String result = null;
		try {
			StringBuffer sb = new StringBuffer();
			XSSFWorkbook xssfWorkbook = new XSSFWorkbook(file.getAbsolutePath());
			int numSheet = xssfWorkbook.getNumberOfSheets();
			if (numSheet > 0) {
				// 获取第一个工作表
				XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);
				// 循环行Row
				for (int rowNum = 0; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
					XSSFRow xssfRow = xssfSheet.getRow(rowNum);
					if (xssfRow == null) {
						continue;
					}
					int cellNum = xssfRow.getLastCellNum();
					if (cellNum > 0) {
						XSSFCell xssfCell = xssfRow.getCell(0);
						String val = getValue(xssfCell);
						if (!CommenUtil.isEmpty(val)) {
							val = val.trim();
							if (!CommenUtil.isEmpty(val)) {
								sb.append(val).append(",");
							}
						}
					}
				}
			}
			// 去除最后一个“，”
			if (sb.length() > 0) {
				result = sb.substring(0, sb.lastIndexOf(","));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 解析文本文件
	 * 
	 * @param file
	 * @return
	 */
	private static String readTxt(File file) {
		String result = null;
		BufferedReader in = null;
		try {
			StringBuffer sb = new StringBuffer();
			in = new BufferedReader(new FileReader(file));
			String line = null;
			while ((line = in.readLine()) != null) {
				if (!CommenUtil.isEmpty(line)) {
					line = line.trim();
					if (!CommenUtil.isEmpty(line)) {
						sb.append(line).append(",");
					}
				}
			}
			// 去除最后一个“，”
			if (sb.length() > 0) {
				result = sb.substring(0, sb.lastIndexOf(","));
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	private static String getValue(HSSFCell hssfCell) {
		if (hssfCell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {
			return String.valueOf(hssfCell.getBooleanCellValue());
		} else if (hssfCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
			return String.valueOf(hssfCell.getNumericCellValue());
		} else {
			return String.valueOf(hssfCell.getStringCellValue());
		}
	}

	private static String getValue(XSSFCell xssfCell) {
		if (xssfCell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) {
			return String.valueOf(xssfCell.getBooleanCellValue());
		} else if (xssfCell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
			return String.valueOf(xssfCell.getNumericCellValue());
		} else {
			return String.valueOf(xssfCell.getStringCellValue());
		}
	}

}
